package com.juc.reentrantlock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest2 {

	private static ReentrantLock lock = new ReentrantLock();
	
	public static void main(String[] args) throws InterruptedException {
		
		Thread t1 = new Thread(()->{
			//如果没有竞争那么可以正常获得锁，
			//如果有竞争则进入阻塞队列，可以被其他线程用interrupt打断
			try {
				//尝试获得锁
				System.out.println("尝试获得锁");
				lock.lockInterruptibly();
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("没有获得锁");
				return;
				
			}
			
			try{
				System.out.println("正常获得锁");
			}finally {
				lock.unlock();
			}
		},"t1");
		
		lock.lock();
		t1.start();
		TimeUnit.SECONDS.sleep(1);
		System.out.println("打断t1");
		t1.interrupt();
		
		
		
	}
}
